using System;
using System.Data;
using System.Text;
using System.Drawing;
using DirectX.Capture;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace Webcam
{
    public partial class Form1 : Form
    {
        private Filters InputOptions = new Filters();
        private Filter VideoInput = null;
        //private Filter AudioInput = null;
        private Capture CaptureInfo = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void ErrorMessage(Exception e)
        {
            MessageBox.Show(e.Message, "Webcam Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            foreach (Filter f in InputOptions.VideoInputDevices)
            {
                comboBox1.Items.Add(f.Name);
            }
            if (comboBox1.Items.Count > 0)
            {
                button1.Enabled = true;
                comboBox1.SelectedIndex = 0;
            }
            /*foreach (Filter f in InputOptions.AudioInputDevices)
            {
                comboBox2.Items.Add(f.Name);
            }
            if (comboBox2.Items.Count > 0)
                comboBox2.SelectedIndex = 0;*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Configure();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                CaptureInfo.CaptureFrame();
                button3.Enabled = true;
                
            }
            catch (Exception exept)
            {
                this.ErrorMessage(exept);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //DialogResult dr = saveFileDialog1.ShowDialog();
            //if (dr == DialogResult.OK)
            //{
                pictureBox2.Image.Save("qr.jpg");
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = true;
            //saveFileDialog1.FileName);
            //}
            //saveFileDialog1.Reset();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            QrTool tool = new QrTool();
            textBox1.Text = tool.Decode();
            button2.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            XmlParser parser = new XmlParser();
            String url = "http://eneide.tabard.fr/query.php?qrcode=14011983";
            //+textBox1.Text;
            String[] tab = new String[20];
            tab = parser.ParseURL(url);
            //for(int i=0;i<tab.Length;i++){
            int i=0;
            while (tab[i] != null)
            {
                comboBox2.Items.Add(tab[i]);
                i++;
            }
            if (comboBox2.Items.Count > 0)
                comboBox2.SelectedIndex = 0;
        }
    }
}